package dao

import (
	"fmt"

	"fawn/blog/common.in/logger"
	"fawn/blog/models"

	"github.com/jinzhu/gorm"
	"go.uber.org/zap"
)

var NoticesDao = newNoticesDao()

type noticesDao struct{}

func newNoticesDao() *noticesDao {
	return &noticesDao{}
}

func (d *noticesDao) TableName() string {
	return "t_notice"
}

// 获取一条消息
func (d *noticesDao) Info(db *gorm.DB) (t models.Notices, err error) {
	err = db.Table(d.TableName()).Order("id asc").First(&t).Error
	if err != nil {
		logger.Logger.Error("mysql",
			zap.String("sql", fmt.Sprintf("%+v", db.QueryExpr())),
			zap.String("error", err.Error()))
		return t, err
	}

	return t, nil
}
